var http = require('http');
// var zlib = require('zlib');

var uinv = {};
uinv.eval = function (data) {
    var Fn = Function; // 一个变量指向Function，防止有些前端编译工具报错
    return new Fn('return ' + data)();
};

/**
 * 将字符串转换为json对象
 */
uinv.jsonParse = function (s, reviver) {
    if (!s || !(typeof s === 'string')) {
        return s;
    }
    var json;
    try {
        json = JSON.parse(s, reviver);
    } catch (e) {
        try {
            json = uinv.eval('(' + s + ')');// 主要用来处理带function的字符串
        } catch (e2) {
            json = s;
        }
    }
    return json;
};

uinv.stringify = function (v, replacer, space) {
    if (!v || !(typeof v === 'object')) {
        return v;
    }
    return JSON.stringify(v, replacer, space);
};

var obj = {'jsonIds': ['300000000000002'], 'endfilters': ['CABINET', 'STANDALONEDEVICE'], 'dcId': '300000000000001'};
var ajaxConfig = {
    // agent: false,
    // rejectUnauthorized: false, //忽略证书错误
    path: '/tarsier-dcv/dcv/asset/getDownDataChainByTerm', //应该包含搜索条件，比如'/index.html?page=12'
    hostname: '192.168.21.56',
    port: '1517',
    protocol: 'http:',
    method: 'post',
    data: obj,
    headers: {
        'token': '691a78fa6a5a1f560df1b057f384f58d0909ca6760cb8ebc86dceebca57fd069ac47d27644be93848d84afebf1cf95bb9607f312dd9a957c0d2d5752145b4627',
        'Content-Type': 'application/json'
        // 'language': 'zh',
        // 'X-Requested-With': 'XMLHttpRequest'
    }
};
console.time('aa');
var req = http.request(ajaxConfig, function (res) {
    const back = [];
    // if (res.headers['content-encoding'] == 'gzip') {
    //     var gzip = zlib.createGunzip();
    //     res.pipe(gzip);
    //     res = gzip;
    // }
    res.setEncoding('utf8');
    res.on('data', function (chunk) {
        if (chunk.toString().includes('�')) {
            // console.log(chunk.toString('utf-8')+'\n');
            // console.log(back.length);
        }
        back.push(chunk);
    });
    res.on('end', function () {
        // console.log(JSON.parse(back));
        console.timeEnd('aa');
        // console.log(uinv.jsonParse(back));
        console.log(back.join(''));
        // console.log(uinv.eval(back));
        // console.log(eval('('+back+')'));
        // console.log(uinv.jsonParse(JSON.stringify(back)));
    });
});
req.on('error', function (e) {
    console.log(e);
});
if (ajaxConfig.data) {
    req.write(JSON.stringify(ajaxConfig.data));
}
req.end();
